class TimerTest extends egret.DisplayObjectContainer {
	timer:egret.Timer;
	ball:egret.Shape;
	lastEndTime:number;
	counterTxt:egret.TextField;
	current:number = 100;
	public constructor() {
		super();
		this.counterTxt = new egret.TextField();
		this.addChild(this.counterTxt);
		this.counterTxt.textColor = 0xff0000;
		
		this.ball = new egret.Shape();
		this.addChild(this.ball);
		this.ball.graphics.beginFill(0xffff00);
		this.ball.graphics.drawCircle(0,0,20);
		this.ball.graphics.endFill();

		this.once(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);

	}
	private onAddToStage():void
	{
		this.timer = new egret.Timer(1000);
		this.timer.addEventListener(egret.TimerEvent.TIMER,this.onTimer,this);
		this.timer.start();

		this.lastEndTime = egret.getTimer(); //获取当前引擎执行时间
		egret.startTick(this.onTickRun,this)  //引擎的滴答回调函数 固定以每秒60帧的速率执行 1000/60
	}
	private onTickRun(timeStamp:number):boolean
	{
		let passMs:number = timeStamp - this.lastEndTime;
		this.lastEndTime = timeStamp;
		let tx:number = 300;
		let ty:number = 400;
		let speed:number = 5;
		let distX:number = tx - this.ball.x;
		let distY:number = ty - this.ball.y;
		let angle:number = Math.atan2(distY,distX);
		let vx:number = Math.cos(angle)*speed;
		let vy:number = Math.sin(angle)*speed;
		this.ball.x += vx;
		this.ball.y += vy;
		//console.log(this.ball.x,this.ball.y);

		//console.log("执行时长:",passMs,1000/60);
		return false; //返回是否重绘
	}
	private onTimer(e:egret.TimerEvent):void{
		//console.log("onTimer",e);
		this.counterTxt.text = "剩余时间:"+this.current;
		this.current--;
	}

}